<template>
  <a-card :bordered="false" class="card-area">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="院系">
              <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" showSearch :filterOption="filterOption" v-model="queryParam.ssyxm" @change="yxChange">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="专业">
              <a-select :allowClear="true" showSearch :filterOption="filterOption" v-model="queryParam.sszym">
                <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh}} {{ i.zymc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="学年">
              <a-select :allowClear="true" v-model="queryParam.xn">
                <a-select-option v-for="i in TYPE0125" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="年级">
              <a-select :allowClear="true" v-model="queryParam.sznj">
                <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item label="学号">
                <a-input v-model="queryParam.xh"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="姓名">
                <a-input v-model="queryParam.xm"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="学生类别">
                <a-select :allowClear="true" v-model="queryParam.xslbm">
                  <a-select-option v-for="i in TYPE0075" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="欠费类型">
                <a-select :allowClear="true" v-model="queryParam.qflx">
                  <a-select-option v-for="i in TYPE0123" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="8" :sm="16">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button type="primary" @click="exportExcel">导出</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {ssyxm: selectMenu.yx.length === 1 ? queryParam.ssyxm : ''}">重置</a-button>
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      :alert="{ show: true}"
      :pagination="{ pageSize: 10 }"
      :scroll="{x: 1600}"
    >
      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
    </s-table>
  </a-card>
</template>

<script>
// import moment from 'moment'
import { STable } from '~'
import { mapState } from 'vuex'

export default {
  name: 'TableList',
  components: {
    STable
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: true,
      // 查询参数
      queryParam: {
        shzt: '02'
      },
      postParams: {},
      // 表头
      columns: [
        {
          title: '学号',
          dataIndex: 'xh',
          width: 140,
          fixed: 'left'
        },
        {
          title: '姓名',
          dataIndex: 'xm'
        },
        {
          title: '性别',
          dataIndex: 'xbmc'
        },
        {
          title: '年级',
          dataIndex: 'sznj'
        },
        {
          title: '学生类别',
          dataIndex: 'xslbmc'
        },
        {
          title: '院系',
          dataIndex: 'ssyxmc'
        },
        {
          title: '专业',
          dataIndex: 'sszymc'
        },
        {
          title: '学年',
          dataIndex: 'xn'
        },
        {
          title: '学费状态',
          dataIndex: 'jnxfzkmc'
        },
        {
          title: '住宿费状态',
          dataIndex: 'jnzsfzkmc'
        },
        {
          title: '两者均欠',
          dataIndex: 'lzsfdqf'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        if(this.selectMenu.yx.length > 0) {
          parameter.pageNum = parameter.pageNo
          let params = Object.assign(this.queryParam, parameter)
          return this.$api.student.arrearList(params).then(res => {
            return {
              data: res.data.rows,
              pageNo: parameter.pageNo,
              pageSize: parameter.pageSize,
              totalPage: Math.ceil(res.data.total / parameter.pageSize),
              totalCount: res.data.total
            }
          })
        }
      },
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      // options: {
      //   alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
      //   rowSelection: {
      //     selectedRowKeys: this.selectedRowKeys,
      //     onChange: this.onSelectChange
      //   }
      // },
      optionAlertShow: true, // 是否显示alert，已选中
      visible: false,
      confirmLoading: false,
      editId: null,
      selectMenu: {
        yx: [], // 院系
        zy: [] // 所属专业
      }
    }
  },
  computed: {
    ...mapState({
      TYPE0126: state => state.app['TYPE0126'], // 年级
      TYPE0075: state => state.app['TYPE0075'], // 学生类别
      TYPE0123: state => state.app['TYPE0123'], // 欠费类型
      TYPE0125: state => state.app['TYPE0125'] // 学年
    })
  },
  created () {
    this.initSelectMenu()
    this.$store.dispatch('app/setDictionary', 'TYPE0123')
    this.$store.dispatch('app/setDictionary', 'TYPE0075')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.$store.dispatch('app/setDictionary', 'TYPE0125')
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        if(this.selectMenu.yx && this.selectMenu.yx.length == 1){
          this.$set(this.queryParam, 'ssyxm', this.selectMenu.yx[0].dwh)
          this.yxChange(this.queryParam.ssyxm)
        }
        this.$nextTick(() => {
          this.$refs.table.refresh(true)
        })
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    yxChange (code) {
      this.$set(this.queryParam, 'sszym', '')
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'zy', [])
      }
    },
    exportExcel () {
      const parameter = {}
      parameter.pageNum = null
      let params = Object.assign(this.queryParam, parameter)
      this.$export('/schoolRoll/xsjfxx/export', {
        ...params
      })
    }
  }
}
</script>
